﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using AdoDAL.Entities;

//using Stiline.DAL.Entities;

namespace AdoDAL.Repositories
{
    public class TaskRepository : BaseRepository
    {
        public void Save(Task item)
        {
            if (item.Id == 0)
            {
                using (var connection = CreateConnection())
                {
                    connection.Open();
                    SqlCommand command = CreateStoredProcedureExecutionCommand("TaskAdd", connection,
                                                                               new SqlParameter("@userId", item.UserId),
                                                                               new SqlParameter("@title", item.Title),
                                                                               new SqlParameter("@description", item.Description),
                                                                               new SqlParameter("@startTime", item.StartTime),
                                                                               new SqlParameter("@endTime", item.EndTime),
                                                                               new SqlParameter("@isPublic", item.IsPublic));
                    command.CommandType = CommandType.StoredProcedure;
                    command.ExecuteNonQuery();
                }
            }
            else
            {
                using (var connection = CreateConnection())
                {
                    connection.Open();
                    SqlCommand command = CreateStoredProcedureExecutionCommand("TaskEdit", connection,
                                                                   new SqlParameter("@id", item.Id),
                                                                   new SqlParameter("@title", item.Title),
                                                                   new SqlParameter("@description", item.Description),
                                                                   new SqlParameter("@startTime", item.StartTime),
                                                                   new SqlParameter("@endTime", item.EndTime),
                                                                   new SqlParameter("@isPublic", item.IsPublic));
                    command.ExecuteNonQuery();
                }
            }
        }

        public IEnumerable<Task> GetAll()
        {
            using (var connection = CreateConnection())
            {
                connection.Open();
                SqlCommand command = CreateStoredProcedureExecutionCommand("TaskLoad", connection);
                SqlDataReader reader = command.ExecuteReader();

                var tasks = new List<Task>();
                while (reader.Read())
                {
                    var task = new Task();
                    task.Id = (int)reader["Id"];
                    task.Title = reader["Title"].ToString();
                    task.Description = reader["Description"].ToString();
                    task.StartTime = DateTime.Parse(reader["StartTime"].ToString());
                    task.EndTime = DateTime.Parse(reader["EndTime"].ToString());
                    task.IsPublic = (bool)reader["IsPublic"];
                    task.UserId = (int)reader["UserId"];

                    tasks.Add(task);
                }

                return tasks;
            }
        }

        public Task Get(int id)
        {
            return GetAll().FirstOrDefault(t => t.Id == id);
        }

        public void Delete(int id)
        {
            using (var connection = CreateConnection())
            {
                connection.Open();
                SqlCommand command = CreateStoredProcedureExecutionCommand("TaskDelete", connection, new SqlParameter("@id", id));

                command.ExecuteNonQuery();
            }
        }

        public void Delete(Task task)
        {
            Delete(task.Id);
        }
    }
}
